# Copyright (c) 2024 Huawei Technologies Co., Ltd.
# This file is a part of the CANN Open Software.
# Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# ======================================================================================================================

include(${METADEF_DIR}/cmake/build_type.cmake)
file(GLOB_RECURSE EXE_GRAPH_SRCS CONFIGURE_DEPENDS "${METADEF_DIR}/exe_graph/*.cc")

######### libexe_graph.so #############
add_library(exe_graph SHARED ${EXE_GRAPH_SRCS})

target_include_directories(exe_graph PRIVATE
    ${METADEF_DIR}
)

target_include_directories(exe_graph PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
    ${CMAKE_BINARY_DIR}
    ${CMAKE_BINARY_DIR}/proto/metadef_protos
)

target_link_libraries(exe_graph PRIVATE
    intf_pub
    mmpa_headers
    metadef_headers
    c_sec
    slog
    $<$<NOT:$<STREQUAL:${TARGET_SYSTEM_NAME},Android>>:-lrt>
    -ldl
)

target_compile_definitions(exe_graph PRIVATE
    google=ascend_private
    $<$<STREQUAL:${ENABLE_OPEN_SRC},True>:ONLY_COMPILE_OPEN_SRC>
    $<IF:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,OS_TYPE=WIN,OS_TYPE=0>
    $<$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>:SECUREC_USING_STD_SECURE_LIB=0 NOMINMAX>
)

target_compile_options(exe_graph PRIVATE
    $<$<STREQUAL:${TARGET_SYSTEM_NAME},Linux>:-O2 -fPIC -Wextra -Wfloat-equal -fno-common>
    $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Debug>>:/MTd>
    $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Release>>:/MT>
)

if (${ENABLE_OPEN_SRC} STREQUAL "True")
    target_link_libraries(exe_graph PRIVATE ascend_protobuf graph_base register)
    target_compile_options(exe_graph PRIVATE -O2)
else()
    target_clone(exe_graph exe_graph_static STATIC)

    target_link_libraries(exe_graph PRIVATE ascend_protobuf graph_base register)
    target_link_libraries(exe_graph_static PRIVATE
            ascend_protobuf_static graph_static register_static
            )

    target_compile_options(exe_graph PRIVATE -O2)
    target_compile_options(exe_graph_static PRIVATE $<$<STREQUAL:${TARGET_SYSTEM_NAME},Linux>:-O2 -fPIC -Wextra -Wfloat-equal>)
endif()

############ exe_meta_device ############
set(STUB_ERROR_MANAGER_SRC
        ${CMAKE_CURRENT_BINARY_DIR}/stub_error_manager.cc
        ${CMAKE_CURRENT_BINARY_DIR}/stub_err_msg.cc
)
add_custom_command(
        OUTPUT ${STUB_ERROR_MANAGER_SRC}
        COMMAND echo "Generating stub files."
        && ${HI_PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../error_manager/stub/gen_stubapi.py ${METADEF_DIR}/inc/common/util ${CMAKE_CURRENT_BINARY_DIR}
        && ${HI_PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../error_manager/stub/gen_stubapi.py ${METADEF_DIR}/inc/external ${CMAKE_CURRENT_BINARY_DIR}
        && echo "Generating stub files end."
)
add_library(exe_meta_device STATIC
        "${METADEF_DIR}/exe_graph/runtime/tiling_data.cc"
        "${METADEF_DIR}/exe_graph/runtime/runtime_attrs.cc"
        "${METADEF_DIR}/exe_graph/runtime/compute_node_info.cc"
        "${STUB_ERROR_MANAGER_SRC}")
target_include_directories(exe_meta_device PRIVATE
        ${CMAKE_CURRENT_LIST_DIR}
        ${METADEF_DIR}/inc/common/util
        ${METADEF_DIR}/inc/external
)
add_library(exe_meta_device_headers INTERFACE)
target_include_directories(exe_meta_device_headers INTERFACE
        $<BUILD_INTERFACE:${METADEF_DIR}/inc>
        $<BUILD_INTERFACE:${METADEF_DIR}/inc/external>
)
target_link_libraries(exe_meta_device PRIVATE intf_pub slog_headers c_sec_headers)
target_link_libraries(exe_meta_device PUBLIC exe_meta_device_headers)
target_compile_options(exe_meta_device PRIVATE
        -O2
        -fPIC
        -Wextra
        -Wfloat-equal
        -D_FORTIFY_SOURCE=2
)

##############################################################
set(STUB_HEADER_LIST
    ${METADEF_DIR}/inc/external/exe_graph/runtime/compute_node_info.h
    ${METADEF_DIR}/inc/external/exe_graph/runtime/runtime_attrs.h
    ${METADEF_DIR}/inc/external/exe_graph/runtime/tiling_data.h
    ${METADEF_DIR}/inc/exe_graph/runtime/device_tiling_context_builder.h
    ${METADEF_DIR}/inc/exe_graph/lowering/bg_kernel_context_extend.h
)

list(TRANSFORM STUB_HEADER_LIST
    REPLACE "^.*/([^/]+)\\.h$" "${CMAKE_CURRENT_BINARY_DIR}/stub_\\1.cc"
    OUTPUT_VARIABLE STUB_SRC_LIST
)

add_custom_command(
    OUTPUT ${STUB_SRC_LIST}
    COMMAND echo "Generating stub files."
            && ${HI_PYTHON} ${METADEF_DIR}/tests/stub/gen_stubapi.py ${CMAKE_CURRENT_BINARY_DIR} ${STUB_HEADER_LIST}
            && echo "Generating stub files end."
)

add_custom_target(exe_graph_stub DEPENDS ${STUB_SRC_LIST})

############ stub/libexe_graph.so ############
add_library(stub_exe_graph SHARED ${STUB_SRC_LIST})

add_dependencies(stub_exe_graph exe_graph_stub)

target_include_directories(stub_exe_graph PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
    ${CMAKE_BINARY_DIR}
    ${CMAKE_BINARY_DIR}/proto/metadef_protos
)

target_compile_options(stub_exe_graph PRIVATE
    -Wfloat-equal
    -fno-common
    -Os
)

target_link_libraries(stub_exe_graph
    PRIVATE
        intf_pub
        c_sec_headers
    PUBLIC
        metadef_headers
)

set_target_properties(stub_exe_graph PROPERTIES
    OUTPUT_NAME exe_graph
    LIBRARY_OUTPUT_DIRECTORY stub
)

############ stub/libexe_graph.a ############
if (NOT ENABLE_OPEN_SRC)
    target_clone(stub_exe_graph stub_exe_graph_static STATIC)

    add_dependencies(stub_exe_graph_static exe_graph_stub)

    target_compile_options(stub_exe_graph_static PRIVATE
        -ffunction-sections
        -fdata-sections
    )
    set_target_properties(stub_exe_graph_static PROPERTIES
        OUTPUT_NAME exe_graph
        ARCHIVE_OUTPUT_DIRECTORY stub
    )
endif ()

############ install ############
install(TARGETS stub_exe_graph OPTIONAL
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/stub
)
